package DAO.Book;


import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.sql.*;
import java.util.ArrayList;

public class BooksDaoImpl implements BooksDao{

    private String IP=this.getIp();
    private String url="jdbc:mysql://"+IP+":3306/mybookssystem?useOldAliasMetadataBehavior=true";
    //自动获取电脑当前IP地址
    //获取ip
    public String getIp(){
        InetAddress addr = null;
        try {
            addr = InetAddress.getLocalHost();
        } catch (UnknownHostException e) {
            e.printStackTrace();
        }
        System.out.println("Local HostAddress: "+addr.getHostAddress());
        String hostname = addr.getHostName();
        System.out.println("Local host name: "+hostname);
        return addr.getHostAddress();
    }


    /*
    查询所有商品信息
     */
    @Override
    public ArrayList<Books> findAll()   {
        ArrayList<Books> list=new ArrayList<>();
        Connection con=null;
        Statement stat=null;
        ResultSet rs =null;
        try {
            //1.导入jar包
            //2.注册驱动
            Class.forName("com.mysql.cj.jdbc.Driver");
            //3.获取连接
            con= DriverManager.getConnection(url,"root","020214YxY");
            //4.获取执行者对象
            stat=con.createStatement();
            //5.执行sql语句，并接收结果
            String sql="SELECT * FROM mybooks ";
            rs = stat.executeQuery(sql);
            //6.处理结果

            while(rs.next()){
                //封装对象
                Books g=new Books();
                g.setBoodsNum(rs.getString("BoodsNum"));
                g.setBoodsName(rs.getString("BoodsName"));
                g.setBoodsPrice(rs.getString("BoodsPrice"));
                g.setBoodsListingDate(rs.getString("BoodsListingDate"));
                g.setBoodsCategories(rs.getString("BoodsCategories"));
                g.setBoodsmargin(rs.getString("Boodsmargin"));
                list.add(g);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            //7.释放资源
            if(con!=null) {
                try {
                    con.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if(rs!=null) {
                try {
                    rs.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if(stat!=null) {
                try {
                    stat.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }

        }

        return list;
    }

    /*
    条件查询，根据GoodsNum查询商品信息
     */
    @Override
    public  Books findByGoodsNum(String BooksNum){
        Books g=new Books();
        Connection con=null;
        Statement stat=null;
        ResultSet rs =null;
        try {
            //1.导入jar包
            //2.注册驱动
            Class.forName("com.mysql.cj.jdbc.Driver");
            //3.获取连接
            con= DriverManager.getConnection(url,"root","020214YxY");
            //4.获取执行者对象
            stat=con.createStatement();
            //5.执行sql语句，并接收结果
            String sql="SELECT * FROM mygoods WHERE BoodsNum= '"+BooksNum+"'";
            rs = stat.executeQuery(sql);
            //6.处理结果

            while(rs.next()){

                g.setBoodsNum(rs.getString("BoodsNum"));
                g.setBoodsName(rs.getString("BoodsName"));
                g.setBoodsPrice(rs.getString("BoodsPrice"));
                g.setBoodsListingDate(rs.getString("BoodsListingDate"));
                g.setBoodsCategories(rs.getString("BoodsCategories"));
                g.setBoodsmargin(rs.getString("Boodsmargin"));

            }
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            //7.释放资源
            if(con!=null) {
                try {
                    con.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if(rs!=null) {
                try {
                    rs.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if(stat!=null) {
                try {
                    stat.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }

        }



        return g;
    }

    /*
    新增商品信息
     */
    @Override
    public  int insert(Books gds){

        Connection con=null;
        Statement stat=null;
        int result=0;//修改的行数
        try {
            //1.导入jar包
            //2.注册驱动
            Class.forName("com.mysql.cj.jdbc.Driver");
            //3.获取连接
            con= DriverManager.getConnection(url,"root","020214YxY");
            //4.获取执行者对象
            stat=con.createStatement();
            //5.执行sql语句，并接收结果
            // SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
            String sql="INSERT INTO mybooks VALUES ('"+gds.getBoodsNum()+"','"+gds.getBoodsName()+"','"+gds.getBoodsPrice()+"','"+gds.getBoodsListingDate()+"','"+gds.getBoodsCategories()+"','"+gds.getBoodsmargin()+"')";
            result=stat.executeUpdate(sql);
            //6.处理结果

        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            //成功弹窗
            new opyes();
            //7.释放资源
            if(con!=null) {
                try {
                    con.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if(stat!=null) {
                try {
                    stat.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }

        }
        return result;
    }

    /*
    修改商品信息
     */
    @Override
    public  int updata(Books gds){
        Connection con=null;
        Statement stat=null;
        int result=0;//修改的行数
        try {
            //1.导入jar包
            //2.注册驱动
            Class.forName("com.mysql.cj.jdbc.Driver");
            //3.获取连接
            con= DriverManager.getConnection(url,"root","020214YxY");
            //4.获取执行者对象
            stat=con.createStatement();
            //5.执行sql语句，并接收结果
            // SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
            String sql="UPDATE mybooks SET BooksNum='"+gds.getBoodsNum()+"',BooksName='"+gds.getBoodsPrice()+"',BooksPrice='"+gds.getBoodsListingDate()+"',BooksListingDate='"+gds.getBoodsCategories()+"',BooksCategories='"+gds.getBoodsCategories()+"',Booksmargin='"+gds.getBoodsmargin()+"'WHERE BooksNum='"+gds.getBoodsNum()+"'";
            result=stat.executeUpdate(sql);
            //6.处理结果
            //   '"+gds.getGoodsNum()+"','"+gds.getGoodsName()+"','"+gds.getGoodsPrice()+"','"+gds.getGoodsListingDate()+"','"+gds.getGoodsCategories()+"','"+gds.getGoodsOrigin()+"'
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            //成功弹窗
            new opyes();
            //7.释放资源
            if(con!=null) {
                try {
                    con.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if(stat!=null) {
                try {
                    stat.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }

        }
        return result;
    }

    /*
    删除商品信息
     */
    public  int delete(String BooksNum){
        Connection con=null;
        Statement stat=null;
        int result=0;//修改的行数
        try {
            //1.导入jar包
            //2.注册驱动
            Class.forName("com.mysql.cj.jdbc.Driver");
            //3.获取连接
            con= DriverManager.getConnection(url,"root","020214YxY");
            //4.获取执行者对象
            stat=con.createStatement();
            //5.执行sql语句，并接收结果
            String sql="DELETE FROM mybooks WHERE BooksNum='"+BooksNum+"'";
            result=stat.executeUpdate(sql);
            //6.处理结果
            //   '"+gds.getGoodsNum()+"','"+gds.getGoodsName()+"','"+gds.getGoodsPrice()+"','"+gds.getGoodsListingDate()+"','"+gds.getGoodsCategories()+"','"+gds.getGoodsOrigin()+"'
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            //成功弹窗
            new opyes();
            //7.释放资源
            if(con!=null) {
                try {
                    con.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if(stat!=null) {
                try {
                    stat.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }

        }
        return result;
    }
    //操作成功弹窗
    class opyes{
        public opyes(){
            // 创建 JFrame 实例
            JFrame frame = new JFrame("系统提示");
            // Setting the width and height of frame
            frame.setSize(350, 200);
            frame.setLocation(500,250);
            frame.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
            frame.setVisible(true);
            JButton j=new JButton("操作成功！！！");
            frame.add(j);
            j.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {

                }
            });

        }
    }

}
